package cn.com.infcn.monitor.task.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Who;

import cn.com.infcn.monitor.bean.IFCUser;
import cn.com.infcn.monitor.task.BaseTask;
import cn.com.infcn.monitor.util.IFCBeanUtils;

/**
 * 获取系统用户相关信息
 * 
 * @author jjs
 *
 */
public class UserTask implements BaseTask {

	private static final Logger LOGGER = Logger.getLogger(UserTask.class);

	@Override
	public List<IFCUser> execute() throws Exception {
		return user();
	}

	private List<IFCUser> user() throws SigarException {

		try {
			Sigar sigar = new Sigar();
			Who whos[] = sigar.getWhoList();
			List<IFCUser> users = null;
			if (whos != null && whos.length > 0) {
				users = new ArrayList<IFCUser>();
				for (int i = 0; i < whos.length; i++) {
					Who who = whos[i];
					IFCUser user = new IFCUser();
					IFCBeanUtils.copyProperties(who, user);
					users.add(user);
				}
			}

			return users;
		} catch (Exception e) {
			LOGGER.warn(e.getMessage(), e);
		}
		return null;
	}

	/*private void copyProperties(Who who, IFCUser user){
		user.setDevice(who.getUser());
		user.setHost(who.getUser());
		user.setTime(who.getTime());
		user.setUser(who.getUser());
	}*/
}
